package com.basic.library.httpmanager.upload;

import android.text.TextUtils;

import com.basic.library.bean.UploadData;
import com.basic.library.bean.UploadResultInfo;
import com.basic.library.httpmanager.task.IView;
import com.basic.library.httpmanager.task.RHttpManager;
import com.basic.library.utils.FileUtils;
import com.basic.library.utils.toast.ToastCompat;
import com.maning.mndialoglibrary.MProgressDialog;

import java.io.File;
import java.util.ArrayList;

import static com.basic.library.httpmanager.task.RHttpManager.obj;


/**
 * Created by YYS on 2018/5/4.
 */

public class UploadImagesHelper {
    private static String UPLOADIMAGE = "http://webapi.service.industrymatrix.old.tz-group.com/files/upload/pro";

    private static UploadImagesHelper uploadImagesHelper;

    public static UploadImagesHelper getInstance() {
        if (uploadImagesHelper == null) {
            uploadImagesHelper = new UploadImagesHelper();
        }
        return uploadImagesHelper;
    }

    public void uploadImage(IView iView, String filepath, String fileName, final OnUploadFinishListener onUploadFinishListener) {
        UploadData uploadData = new UploadData();
        try {
            uploadData.setInput(FileUtils.encodeBase64File(filepath));
        } catch (Exception e) {
            e.printStackTrace();
        }
        uploadData.setACL(false);
        uploadData.setName(fileName);
        uploadData.setPath("img/");
        obj().postbodyAsync(iView, UPLOADIMAGE, uploadData, new RHttpManager.ResultCallback<UploadResultInfo>() {

            @Override
            public void onResponse(UploadResultInfo response) {
                if (onUploadFinishListener != null)
                    onUploadFinishListener.onSuccess(response);
            }

            @Override
            public void onError(Throwable e) {
                ToastCompat.show("图片上传失败");
                MProgressDialog.dismissProgress();
            }
        });
    }


    /**
     * 串行上传图片集
     *
     * @param iView
     * @param filepath               图片本地路径集合 ，可传入空url
     * @param resultInfos            返回结果,顺序和传入url一致，每次上传哪张图片根据resultInfos.size()判断
     * @param onUploadFinishListener
     */
    public void uploadImages(final IView iView, final ArrayList<String> filepath, ArrayList<UploadResultInfo> resultInfos, final OnUploadFinishListener onUploadFinishListener) {
        UploadData uploadData = new UploadData();
        if (resultInfos == null)
            resultInfos = new ArrayList<>();
        try {
            String path = filepath.get(resultInfos.size());
            if (TextUtils.isEmpty(path)) { //空路径，返回null。做到下标一一对应
                resultInfos.add(null);
                if (resultInfos.size() == filepath.size()) {
                    if (onUploadFinishListener != null)
                        onUploadFinishListener.onSuccess(resultInfos.toArray(new UploadResultInfo[0]));
                } else {
                    uploadImages(iView, filepath, resultInfos, onUploadFinishListener);
                }
                return;
            } else if (path.startsWith("http")) {
                UploadResultInfo resultInfo = new UploadResultInfo();
                resultInfo.setUrl(path);
                resultInfos.add(resultInfo);
                if (resultInfos.size() == filepath.size()) {
                    if (onUploadFinishListener != null)
                        onUploadFinishListener.onSuccess(resultInfos.toArray(new UploadResultInfo[0]));
                } else {
                    uploadImages(iView, filepath, resultInfos, onUploadFinishListener);
                }
                return;
            }
            uploadData.setInput(FileUtils.encodeBase64File(path));
            uploadData.setACL(false);
            uploadData.setName(new File(path).getName());
            uploadData.setPath("img/");
        } catch (Exception e) {
            e.printStackTrace();
        }
        final ArrayList<UploadResultInfo> finalResultInfos = resultInfos;
        obj().postbodyAsync(iView, UPLOADIMAGE, uploadData, new RHttpManager.ResultCallback<UploadResultInfo>() {

            @Override
            public void onResponse(UploadResultInfo response) {
                response.setPositionInList(finalResultInfos.size());
                finalResultInfos.add(response);
                if (finalResultInfos.size() == filepath.size()) {
                    if (onUploadFinishListener != null)
                        onUploadFinishListener.onSuccess(finalResultInfos.toArray(new UploadResultInfo[0]));
                } else {
                    uploadImages(iView, filepath, finalResultInfos, onUploadFinishListener);
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastCompat.show("图片上传失败");
                MProgressDialog.dismissProgress();
                if (onUploadFinishListener != null)
                    onUploadFinishListener.onFail(finalResultInfos.toArray(new UploadResultInfo[0]));
            }
        });
    }

    public abstract static class OnUploadFinishListener {
        public abstract void onSuccess(UploadResultInfo... response);

        public void onFail(UploadResultInfo... response) {
        }
    }

}